﻿using System.Web.Http;
using Castle.Core.Logging;
using Hiver.Util.Config;
using Hiver.Util.Config.Model;
using Hiver.WebApi.Models;
using Hiver.Business;
using Hiver.Entity;

namespace Hiver.WebApi.Controllers
{
    /// <summary>
    /// 登录控制器
    /// </summary>
    public class LoginController : ApiController
    {
        /// <summary>
        /// 日志
        /// </summary>
        public ILogger Logger { get; set; }

        /// <summary>
        /// 用户服务
        /// </summary>
        public IUserService service { get; set; }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="model">用户登录模型</param>
        /// <returns></returns>
        [Route("api/Login")]
        [HttpPost]
        public BaseModel<bool> Index([FromBody] LoginModel model)
        {
            BaseModel<bool> result = new BaseModel<bool>();
            UserInfo userInfo = service.Login(model.userName, model.passWord);
            if (userInfo != null && userInfo.Id > 0)
            {
                result.msg = "登录成功";
                result.data = true;
            }
            else
            {
                result.msg = "登录失败";
                result.data = false;
            }
            return result;
        }

        /// <summary>
        /// 获得配置信息
        /// </summary>
        /// <returns></returns>
        [Route("api/Config")]
        [HttpPost]
        public ApplicationConfigModel Config()
        {
            return YamlHelper<ApplicationConfigModel>.Load();
        }
    }
}